/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import model.HoSo;
import model.KtKl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class KT_KLDao {
    private static Session session;
    
    public KT_KLDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertKtKl
     */
    public boolean insertKtKl(String soQuyetDinh, String maNV, String loaiDaiNgo, String hinhThucDaiNgo, Date ngayQuyetDinh, String ghiChu){
        try {
            session.beginTransaction();
            
            HoSo hoSo = (HoSo)session.get(HoSo.class, maNV);
            KtKl ktKl = new KtKl(soQuyetDinh, hoSo, loaiDaiNgo, hinhThucDaiNgo, ngayQuyetDinh, ghiChu);
            
            hoSo.getKtKls().add(ktKl);
            
            session.save(ktKl);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updateKtKl
     */
    public boolean updateKtKl(String soQuyetDinh, String maNV, String loaiDaiNgo, String hinhThucDaiNgo, Date ngayQuyetDinh, String ghiChu){
        try {
            session.beginTransaction();
            
            HoSo hoSo = (HoSo) session.get(HoSo.class, maNV);
            KtKl ktKl = (KtKl)session.get(KtKl.class, soQuyetDinh);
            ktKl.setGhiChu(ghiChu);
            ktKl.setHinhThucDaiNgo(hinhThucDaiNgo);
            ktKl.setHoSo(hoSo);
            ktKl.setLoaiDaiNgo(loaiDaiNgo);
            ktKl.setNgayQuyetDinh(ngayQuyetDinh);
            
            hoSo.getKtKls().add(ktKl);
            
            session.update(ktKl);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * deleteKtKl
     */
    public boolean deleteKtKl(String id){
        try {
            session.beginTransaction();
            
            KtKl ktKl = (KtKl)session.get(KtKl.class, id);
            
            session.delete(id);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
}
